package DAO;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Beans.ProovedorBean;
import Beans.ProovedorBean;
import Controller.AdministradorProovedor;
import HibernateUtils.HibernateUtil;
import Model.Proovedor;


public class HibernateProveedorDAO {
	private static HibernateProveedorDAO instancia = null;
	private static SessionFactory sf = null;

	public static HibernateProveedorDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateProveedorDAO();
		} 
		return instancia;
	}
	
	public void grabarProveedor(ProovedorBean v){
		
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(v);;
		session.flush();
		session.getTransaction().commit();
		session.close();
		
		
	}

	public ProovedorBean getProveedor(ProovedorBean p) {
		Session session = sf.openSession();
		Query q=session.createQuery("from ProovedorBean v where v.idProveedor=? ").setInteger(0, p.getIdProveedor());
		q.setFirstResult(0).setMaxResults(1);
		Object o=q.uniqueResult();
		ProovedorBean proveedor=(ProovedorBean)o;
		return proveedor;
		
		
		
	}

	public void updateProveedor(ProovedorBean vb) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.update(vb);;
		session.flush();
		session.getTransaction().commit();
		session.close();
		
	}
}
